package countor;

import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 并发大数计数器计数，宇宙计数器，最大计数：一切都安静了，数值已没有意义，看你有多少资源
 * @author dingnate
 */
public class AtomBigIntegerCountor<T> implements ICountor<T> {
	private T owner;
	// Atom实现方式
	private AtomicReference<BigInteger> counter = new AtomicReference<BigInteger>(BigInteger.ZERO);

	public AtomBigIntegerCountor(T owner) {
		super();
		this.owner = owner;
	}

	public final void incr() {
		incr(BigInteger.ONE);
	}

	public final void incr(BigInteger bigInt) {
		counter.accumulateAndGet(bigInt, (p, x) -> p.add(x));
	}

	public final BigInteger getCount() {
		return counter.get();
	}

	public final T getOwner() {
		return owner;
	}

	@Override
	public String toString() {
		return "Counter [" + "count=" + getCount() + ", owner=" + owner + "]";
	}
}